package ex.com.jm.food.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import ex.com.jm.food.entity.CompanyProfitSummary;
import ex.com.jm.food.entity.DateProfitSummary;
import ex.com.jm.food.entity.MenuProfitSummary;


public class ExcelUtil {

	private String path=null;
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public  static void CPSExcel(List<CompanyProfitSummary> datas,String[] headers,String[] columns,OutputStream out) throws Exception{
		//String[] headers=new String[]{"通知编号","通知标题","发布日期"};
		//导出一个excel首先是写一个文件
		//创建一个excel表结构的文件
		HSSFWorkbook demoWorkBook=new HSSFWorkbook();
		//创建一个sheet(页)
		HSSFSheet sheet=demoWorkBook.createSheet();
		//创建一个行，第一个行创建表头
		HSSFRow header=sheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell=header.createCell(i);
			cell.setCellValue(headers[i]);
		}
		for (int i = 0; i < datas.size(); i++) {
			Object data=datas.get(i);
			HSSFRow row=sheet.createRow(i+1);
			for (int j = 0; j < columns.length; j++) {
				String postfix=columns[j].substring(0, 1).toUpperCase()+columns[j].substring(1);
			    Method m=data.getClass().getDeclaredMethod("get"+postfix);
			    HSSFCell cell=row.createCell(j);
			    Object val=m.invoke(data);
				cell.setCellValue(val.toString());
			}
		}
		try {
			demoWorkBook.write(out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public  static void MPSExcel(List<MenuProfitSummary> datas,String[] headers,String[] columns,OutputStream out) throws Exception{
		//String[] headers=new String[]{"通知编号","通知标题","发布日期"};
		//导出一个excel首先是写一个文件
		//创建一个excel表结构的文件
		HSSFWorkbook demoWorkBook=new HSSFWorkbook();
		//创建一个sheet(页)
		HSSFSheet sheet=demoWorkBook.createSheet();
		//创建一个行，第一个行创建表头
		HSSFRow header=sheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell=header.createCell(i);
			cell.setCellValue(headers[i]);
		}
		for (int i = 0; i < datas.size(); i++) {
			Object data=datas.get(i);
			HSSFRow row=sheet.createRow(i+1);
			for (int j = 0; j < columns.length; j++) {
				String postfix=columns[j].substring(0, 1).toUpperCase()+columns[j].substring(1);
			    Method m=data.getClass().getDeclaredMethod("get"+postfix);
			    HSSFCell cell=row.createCell(j);
			    Object val=m.invoke(data);
				cell.setCellValue(val.toString());
			}
		}
		try {
			demoWorkBook.write(out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public  static void DPSExcel(List<DateProfitSummary> datas,String[] headers,String[] columns,OutputStream out) throws Exception{
		//String[] headers=new String[]{"通知编号","通知标题","发布日期"};
		//导出一个excel首先是写一个文件
		//创建一个excel表结构的文件
		HSSFWorkbook demoWorkBook=new HSSFWorkbook();
		//创建一个sheet(页)
		HSSFSheet sheet=demoWorkBook.createSheet();
		//创建一个行，第一个行创建表头
		HSSFRow header=sheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell=header.createCell(i);
			cell.setCellValue(headers[i]);
		}
		for (int i = 0; i < datas.size(); i++) {
			Object data=datas.get(i);
			HSSFRow row=sheet.createRow(i+1);
			for (int j = 0; j < columns.length; j++) {
				String postfix=columns[j].substring(0, 1).toUpperCase()+columns[j].substring(1);
			    Method m=data.getClass().getDeclaredMethod("get"+postfix);
			    HSSFCell cell=row.createCell(j);
			    Object val=m.invoke(data);
				cell.setCellValue(val.toString());
			}
		}
		try {
			demoWorkBook.write(out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
}
